package com.vehicle.biz.core.support.impl;

import com.vehicle.biz.core.support.PlateDetectSupport;
import com.vehicle.biz.core.support.abs.AbstractSupport;
import com.vehicle.domains.ctx.PlateDetectContext;
import com.vehicle.domains.entity.CommonVehicleEntity;
import com.vehicle.domains.entity.TransportVehicleEntity;
import com.vehicle.domains.entity.VehicleCommonLedgerEntity;
import com.vehicle.domains.entity.VehicleTransportLedgerEntity;
import com.vehicle.enums.EmissionStandardEnum;
import com.vehicle.enums.GovUploadStatus;
import com.vehicle.enums.VehicleTypeEnum;
import com.vehicle.exception.VehicleAccessException;
import com.vehicle.service.TransportVehicleService;
import com.vehicle.service.VehicleCommonLedgerService;
import com.vehicle.service.VehicleTransportLedgerService;
import com.vehicle.service.config.SystemConfig;
import com.vehicle.service.config.SystemConfigService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class TransportSupport extends AbstractSupport {
    @Resource
    private TransportVehicleService transportVehicleService;
    @Resource
    private SystemConfigService systemConfigService;
    @Resource
    private VehicleTransportLedgerService vehicleTransportLedgerService;
    @Resource
    private VehicleCommonLedgerService vehicleCommonLedgerService;
    public String getSupportName() {
        return VehicleTypeEnum.TRANSPORT.name();
    }
    @Override
    public void processIn(PlateDetectContext plateDetectContext) {
        super.processIn(plateDetectContext);

        CommonVehicleEntity commonVehicleEntity = plateDetectContext.getCommonVehicleEntity();
        //查询出运输车辆信息
        TransportVehicleEntity transportVehicleEntity = transportVehicleService.getTransportVehicleById(commonVehicleEntity.getAssociationId());
        if (transportVehicleEntity == null) {
            throw VehicleAccessException.DATA_NOT_EXIST.newInstance("运输车辆不存在");
        }
        plateDetectContext.setTransportVehicleEntity(transportVehicleEntity);
        //判断车辆是否在黑名单中

        //判断车辆排放标准是否符合要求
        EmissionStandardEnum emissionStandardEnum = EmissionStandardEnum.valueOf(systemConfigService.get(SystemConfig.emissionStandard));
        if (emissionStandardEnum.getIndex() < EmissionStandardEnum.valueOf(transportVehicleEntity.getEmissionStandard()).getIndex()) {
            throw VehicleAccessException.SYSTEM_ERROR.newInstance("车辆排放标准不符合要求");
        }

        //是否允许运输车辆进入
        if (!systemConfigService.get(SystemConfig.transportEntryControl)) {
            throw VehicleAccessException.SYSTEM_ERROR.newInstance("不允许运输车辆进入");
        }

        log.info("运输车辆可以进入：{}", transportVehicleEntity);
    }

    @Override
    public void processOut(PlateDetectContext plateDetectContext) {
        CommonVehicleEntity commonVehicleEntity = plateDetectContext.getCommonVehicleEntity();
        if (commonVehicleEntity.getAssociationId() == null) {
            throw VehicleAccessException.DATA_NOT_EXIST.newInstance("车辆未关联运输车辆");
        }
        TransportVehicleEntity transportVehicleEntity = transportVehicleService.getTransportVehicleById(commonVehicleEntity.getAssociationId());
        if (transportVehicleEntity == null) {
            throw VehicleAccessException.DATA_NOT_EXIST.newInstance("运输车辆不存在");
        }
    }

    @Override
    public void recordIn(PlateDetectContext plateDetectContext) {
        super.recordIn(plateDetectContext);
        VehicleTransportLedgerEntity vehicleTransportLedgerEntity = new VehicleTransportLedgerEntity();
        vehicleTransportLedgerEntity.setVehicleNumber(plateDetectContext.getVehicleNumber());

        TransportVehicleEntity transportVehicleEntity = plateDetectContext.getTransportVehicleEntity();
        vehicleTransportLedgerEntity.setTransportVehicleType(transportVehicleEntity.getTransportVehicleType());

        VehicleCommonLedgerEntity vehicleCommonLedgerEntity = plateDetectContext.getVehicleCommonLedgerEntity();
        vehicleTransportLedgerEntity.setEntryPlateImgUrl(vehicleCommonLedgerEntity.getEntryPlateImgUrl());
        vehicleTransportLedgerEntity.setEntryVehicleImgUrl(vehicleCommonLedgerEntity.getEntryVehicleImgUrl());
        vehicleTransportLedgerEntity.setEntryCameraIp(vehicleCommonLedgerEntity.getEntryCameraIp());
        vehicleTransportLedgerEntity.setEntryTime(vehicleCommonLedgerEntity.getEntryTime());
        vehicleTransportLedgerEntity.setEntryUploadStatus(GovUploadStatus.INIT.name());

        vehicleTransportLedgerService.insert(vehicleTransportLedgerEntity);

        vehicleCommonLedgerService.updateAssId(vehicleCommonLedgerEntity.getId(), vehicleTransportLedgerEntity.getId());
    }

    @Override
    public void recordOut(PlateDetectContext plateDetectContext) {
        super.recordOut(plateDetectContext);

        VehicleCommonLedgerEntity vehicleCommonLedgerEntity = plateDetectContext.getVehicleCommonLedgerEntity();
        VehicleTransportLedgerEntity vehicleTransportLedgerEntity = new VehicleTransportLedgerEntity();
        vehicleTransportLedgerEntity.setVehicleNumber(plateDetectContext.getVehicleNumber());
        TransportVehicleEntity transportVehicleEntity = plateDetectContext.getTransportVehicleEntity();
        vehicleTransportLedgerEntity.setTransportVehicleType(transportVehicleEntity.getTransportVehicleType());

        vehicleTransportLedgerEntity.setExitPlateImgUrl(vehicleCommonLedgerEntity.getExitPlateImgUrl());
        vehicleTransportLedgerEntity.setExitVehicleImgUrl(vehicleCommonLedgerEntity.getExitVehicleImgUrl());
        vehicleTransportLedgerEntity.setExitCameraIp(vehicleCommonLedgerEntity.getExitCameraIp());
        vehicleTransportLedgerEntity.setExitTime(vehicleCommonLedgerEntity.getExitTime());
        vehicleTransportLedgerEntity.setExitUploadStatus(GovUploadStatus.INIT.name());

        if (vehicleCommonLedgerEntity.getAssociationId() == null) {
            vehicleTransportLedgerService.insert(vehicleTransportLedgerEntity);
            vehicleCommonLedgerService.updateAssId(vehicleCommonLedgerEntity.getId(), vehicleTransportLedgerEntity.getId());
        } else {
            vehicleTransportLedgerEntity.setId(vehicleCommonLedgerEntity.getAssociationId());
            vehicleTransportLedgerService.updateTransportExitInfo(vehicleTransportLedgerEntity);
        }
        plateDetectContext.setTransportVehicleEntity(transportVehicleEntity);
    }
}
